import java.util.List;
import java.util.Map;

import junit.framework.Assert;

import org.junit.Before;
import org.junit.Test;

import com.dragonlance.beans.User;
import com.dragonlance.dao.UserDao;
public class UserDaoTest {
	private UserDao dao;
	@Before
	public void testDaoImpl() {
		dao = new UserDao();
	}
//	@Test
	public void testFindAll() {
		List<User> result = dao.findAll(User.class);
		Assert.assertEquals(result.size(), 1);
	}
	@Test
	public void testFindById() {
		User user = dao.findById(User.class, 1);
		Assert.assertEquals(user.getNickName(), "newwicki");
	}
	@Test
	public void testExecuteQuery() {
		List<Map<String, Object>> result = dao.executeQuery("select * from user where UID=?", new Object[] { 1 });
		Assert.assertEquals(result.get(0).get("NickName"), "newwicki");
	}
//	@Test
	public void testInsert() {
		long id = dao.insert("insert into user (NickName) values (?)", new Object[] { "男" });
		Assert.assertNotNull(id);
	}
//	@Test
	public void testUpdate() {
		long id = dao.update("update User set NickName=? where UID=?", new Object[] { "newwicki", 1 });
		Assert.assertEquals(id, 1);
	}
//	@Test
	public void testDelete() {
		dao.delete("delete from User where UID=?", new Object[] { 5 });
	}
	// @Test
	public void testExecuteUpdate() {
	}
//	@Test
	public void testFindByUserName() {
		User user = dao.findByUserName("wicki");
		Assert.assertEquals(user.getNickName(), "newwicki");
	}
	//@Test
	public void testBatchUpdate() {
		Object[][] objs = new Object[][] { { "man" }, { "women" }, { "man" } };
		int[] ids = dao.batchUpdate("insert into user (NickName) values(?)", objs);
		Assert.assertEquals(3, ids.length);
	}
	
	@Test  
    public void testSave(){   
		User user = dao.findById(User.class, 1);
		user.setNickName("fortest002");
		user.save();
		user = dao.findById(User.class, 1);
		Assert.assertEquals(user.getNickName(), "fortest001");
    }   
}
